package com.qf.p1.service.impl;

import com.qf.p1.dao.CourseDao;
import com.qf.p1.dao.CourseMapper;
import com.qf.p1.dao.StuCourseDao;
import com.qf.p1.dao.UserDao;
import com.qf.p1.model.*;
import com.qf.p1.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户业务逻辑的实现
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.04.06
 * @since 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private CourseDao courseDao;

    @Resource
    private StuCourseDao stuCourseDao;

    @Resource
    private CourseMapper courseMapper;

    @Override
    public User signIn(String username, String password) {

        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User> users = userDao.selectByExample(example);
        if(users !=null&&users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public boolean findUserName(String username) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userDao.selectByExample(example);
        if (users !=null&&users.size()>0){
           return true;
        }
        return false;
    }

    @Override
    public void signUp(String username, String password ,String gender) {
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setGender(gender);
        //注册一律是学生用户
        user.setRole(0);
        userDao.insertSelective(user);
    }

    @Override
    public boolean selectCourse(Integer uid, Integer cid) {

        StuCourseExample example=new StuCourseExample();
        StuCourseExample.Criteria criteria=example.createCriteria();
        criteria.andSidEqualTo(uid);
        long count = stuCourseDao.countByExample(example);
        if (count>=5){
            return false;
        }
        criteria.andCidEqualTo(cid);
        List<StuCourse> stuCourses = stuCourseDao.selectByExample(example);

        if (stuCourses!=null&&stuCourses.size()>0){
            //选过了
            return true;
        }else {
            //update
            Course course = courseDao.selectByPrimaryKey(cid);
            //让选课人数+1
            course.setsSum(course.getsSum()+1);
            //更新
            courseDao.updateByPrimaryKeySelective(course);
            //insert
            StuCourse stuCourse=new StuCourse();
            stuCourse.setSid(uid);
            stuCourse.setCid(cid);
            stuCourseDao.insertSelective(stuCourse);
            return true;
        }
    }

    @Override
    public boolean unSelectCourse(Integer userId, Integer cid) {
        Course course = courseDao.selectByPrimaryKey(cid);
        //选课的人数-1
        course.setsSum(course.getsSum()-1);
        courseDao.updateByPrimaryKeySelective(course);
        StuCourseExample stuCourseExample=new StuCourseExample();
        StuCourseExample.Criteria criteria = stuCourseExample.createCriteria();
        criteria.andSidEqualTo(userId);
        criteria.andCidEqualTo(cid);
        stuCourseDao.deleteByExample(stuCourseExample);
        return true;
    }

    @Override
    public List<CourseTname> findCourses(Integer uid) {

        return courseMapper.selectUserId(uid);


    }

}
